import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login.vue'
import home from '../views/home.vue'
import Welcome from '../views/Welcome.vue'
import Users from '../views/Users.vue'
import Rights from '../views/rights.vue'
import Roles from '../views/roles.vue'
import Categories from '../views/categories.vue'
import GoodsList from '../views/goodsList.vue'
import Goodsadd from '../views/Goodsadd.vue'
import Orders from '../views/orders.vue'
import Reports from '../views/reports.vue'
import Params from '@/views/params.vue'
Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
},
{
    path: '/login',
    component: login
},
{
    path: '/home',
    component: home,
    redirect: '/welcome',
    children: [{
        path: '/welcome',
        component: Welcome
    },
    {
        path: '/users',
        component: Users
    },
    {
        path: '/rights',
        component: Rights
    },
    {
        path: '/Roles',
        component: Roles
    },
    {
        path: '/categories',
        component: Categories
    }, {
        path: '/goods',
        component: GoodsList
    }, {
        path: '/goods/add',
        component: Goodsadd
    }, {
        path: '/orders',
        component: Orders
    }, {
        path: '/reports',
        component: Reports
    }, {
        path: '/params',
        component: Params
    }
    ]
}
]

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    const tokenstr = window.sessionStorage.getItem('token')
    if (!tokenstr) return next('/login')
    next()
})
export default router
